<template>
    <div class="widgets bg-white">
        <el-input v-model.trim="searchText" placeholder="输入搜索内容" class="search__input" @keyup.enter.native="search"></el-input>
        <el-button type="primary" @click="search">查询</el-button>
        <div class="btn-group right">
            <el-button type="info" @click="refresh">刷新</el-button>
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>删除</el-button>
            <el-button type="danger" v-else @click="del">删除</el-button>
            <el-button type="danger" v-if="multipleSelection.length <= 0" disabled>禁用</el-button>
            <el-button type="danger" v-else @click="disabedBtn(0, '禁用')">禁用</el-button>
            <el-button type="success" v-if="multipleSelection.length <= 0" disabled>启用</el-button>
            <el-button type="success" v-else @click="disabedBtn(1, '启用')">启用</el-button>
            <el-button-group class="btn-other">
                <el-button v-if="multipleSelection.length <= 0" disabled>
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导出" v-else @click="exportExcel">
                    <img src="../../../assets/images/export.png" alt="">
                </el-button>
                <el-button title="导入">
                    <el-upload class="upload-demo" :action="$url + 'closeAccount/importCloseAccount?systemId=' + systemId" :show-file-list="false" multiple style="display: inline-block;" :on-success="importExcel">
                        <img src="../../../assets/images/import.png" alt="">
                    </el-upload>
                </el-button>                 
                <el-button title="打印">
                    <img src="../../../assets/images/print.png" alt="">
                </el-button>
                <el-button title="帮助">
                    <img src="../../../assets/images/help.png" alt="">
                </el-button>
            </el-button-group>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            searchText: '',
            multipleSelection: this.$store.state.settlementWayStore.multipleSelection,
            selectRowId: this.$store.state.settlementWayStore.selectRowId
        }
    },
    computed: {
        systemId() {
            return this.$store.state.systemId;
        }
    },
    watch: {
        searchText(newVal) {
            if(!newVal) {
                this.$store.dispatch('settlementWayStore/getTableData');
            }
        }
    },
    methods: {
        search() {          //搜索
            if(this.searchText) {
                let searchTableData = this.$store.state.settlementWayStore.searchTableData,
                    newArr = [],
                    checkKey = ['name', 'toNamePinyin', 'toSimplePinyin'];
                if(searchTableData.length > 0) {
                    searchTableData.forEach(item => {
                        if((item['name'] && item['name'].indexOf(this.searchText) > -1) || (item['toNamePinyin'] && item['toNamePinyin'].indexOf(this.searchText) > -1) || (item['toSimplePinyin'] && item['toSimplePinyin'].indexOf(this.searchText) > -1)) {
                            newArr.push(item);
                        }
                    });
                }
                if(newArr.length > 0) {
                    this.$store.commit('settlementWayStore/updateTableData', newArr);
                } else {
                    this.$store.commit('settlementWayStore/updateTableData', []);
                }
            }
        },
        refresh() {         //刷新
            const loading = this.$loading({
                lock: true,
                text: '数据刷新中...',
                spinner: 'el-icon-loading',
                background: '#fff'
            });
            setTimeout(() => {
                loading.close();
            }, 1000);
        },
        add() {             //新增
            this.$store.commit('settlementWayStore/updateRowId');
            this.$util.openLayerIframe({title: '结算方式-新增', zIndex: 13, width: '450px', height: '290px', url: this.$domain + '/#/basicData/settlementWay/form'});
        },
        del() {             //删除
            this.$confirm('确定删除选中数据？', '删除提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                var type = {
                    headers: {
                        'Content-Type': 'application/json'
                    }
                }
                this.$http.post('closeAccount/deleteCloseAccount', JSON.stringify(this.selectRowId), type).then(res => {
                    if(res.data.code == 20001) {
                        this.$message({message: '删除成功', type: 'success', duration: 1500});
                        setTimeout(() => {
                            this.$store.dispatch('settlementWayStore/getTableData');
                        }, 1000);
                    } else {
                        this.$message.error(res.data.msg);
                    }
                });
            }).catch(() => {
                this.$message({message: '已取消删除', type: 'info', duration: 1500});
            });
        },
        disabedBtn(status, msg) {           //禁用、启用
            var type = {
                headers: {
                    'Content-type': 'application/json;charset=UTF-8'
                }
            }
            for(var i = 0; i < this.multipleSelection.length; i++) {
                if(this.multipleSelection[i]['state'] == status) {
                    this.$alert(msg + '失败，其中包含已'+ msg +'选项，重新选择！', msg + '提示', {
                        confirmButtonText: '确定'
                    });
                    return false;
                }
            }
            this.$http.post('closeAccount/startOrDisableCurrency?state=' + status, JSON.stringify(this.selectRowId), type).then(res => {
                if(res.data.code == 20001) {
                    this.$message({message: msg + '成功', type: 'success', duration: 1500});
                    setTimeout(() => {
                        this.$store.dispatch('settlementWayStore/getTableData');
                    }, 1000);
                } else {
                    this.$message({message: res.data.msg || msg + '失败', type: 'error', duration: 1500});
                }
            });
        },
        exportExcel() {         //导出
            var idSting = this.selectRowId.join(',');           //将[1,2,3]格式转为 "1,2,3"
            this.$http.get('closeAccount/exportCloseAccount',{params: {idList: idSting}}).then(res => {
                this.$message({message: '导出成功', type: 'success', duration: 1500});
                window.location.href = this.$url + 'closeAccount/exportCloseAccount?idList=' + idSting;          //导出时触发
            }).catch(err => {
                this.$message.error(err || '导出异常');
            });
        },
        importExcel(res) {      //导入
            if(res.code === 20001) {        //导入成功后回调该函数
                this.$message({message: '导入成功', type: 'success', duration: 1500});
                setTimeout(() => {
                    this.$store.dispatch('settlementWayStore/getTableData');
                }, 1000);
            } else {
                this.$message.error(res.msg || '导入失败');
            }
        }
    },
}
</script>